﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

namespace DentistOffice
{
    public partial class WyslijSub : System.Web.UI.Page
    {
        List<String> adresy = new List<String>();
        protected void Page_Load(object sender, EventArgs e)
        {
            subskrypcja.Text = "";
            DentistEntities DE = new DentistEntities();
            var query = from sub in DE.Subscriptions select sub;
            int i = 0;
            foreach (var visit in query)
            {
                if (visit.email.IndexOf("@") != -1)
                {
                    i++;
                    adresy.Add(visit.email);
                    subskrypcja.Text += "<b>" + i + ":</b> " + visit.email + "<br />";
                }
            }
        }
        protected void wyslijSub(object sender, EventArgs e)
        {
            try
            {
                MailMessage email = new MailMessage();
                MailAddress fromEmail = new MailAddress("miasi@o2.pl");
                email.From = fromEmail;
                foreach (string akt in adresy)
                    email.To.Add(akt);
                email.Subject = temats.Text;
                email.IsBodyHtml = true;
                email.Body = trescs.Text;
                SmtpClient client = new SmtpClient("poczta.o2.pl", 587);
                System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("miasi", "kopytko");
                client.UseDefaultCredentials = false;
                client.Credentials = SMTPUserInfo;
                client.Send(email);
                danep.Visible = true;
                dane.Text = "Wiadomość została wysłana!";
                trescs.Text = "";
                temats.Text = "";
            }
            catch (Exception ex)
            {
                danep.Visible = true;
                dane.Text = "Wiadomość nie została wysłana! Wystąpił następujący błąd: " + ex;
            }
        }
        protected void Reset(object sender, EventArgs e)
        {
            trescs.Text = "";
            temats.Text = "";
        }
    }
}